package search.service.graph;

import com.fasterxml.jackson.databind.ObjectMapper;
import common.model.graph.CommonSchema;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import common.helper.json.JsonFormatter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Common on 9/7/16.
 */
public class GraphServiceHelper {
    static final Logger logger = LoggerFactory.getLogger(EntityExpansionService.class);

    public static void printAllProperties(Vertex vertex){
        Iterator<VertexProperty<Object>> ps = vertex.properties();
        while (ps.hasNext()){
            VertexProperty<Object> p = ps.next();
            logger.debug("-- [property] key = " + p.key() + ", value = " + p.value().toString());
        }
    }

    public static String getNameZh(Vertex vertex){
        return vertex.value(CommonSchema.Properties.has_name_zh);
    }

    public static boolean insertValueByKey(Map<String, List<String> > map, String key, String value){
        if( !map.containsKey(key) )
            map.put(key, new ArrayList<String>() );
        map.get(key).add( value );
        return true;
    }

    public static String outputMapToJson( Map<String, List<String> > map){
        ObjectMapper mapper = JsonFormatter.getOutputObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString( map );
        }
        catch (Exception e){
            logger.error("write Map to Json failed!");
        }
        return json;
    }

    public static String outputMapMapToJson( Map<String, Map<String, List<String>> > map){
        ObjectMapper mapper = JsonFormatter.getOutputObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString( map );
        }
        catch (Exception e){
            logger.error("write Map to Json failed!");
        }
        return json;
    }



}
